<?php
/* 
 *************************************************************************
 *                                                                       *
 * Product: Płatności.pl for OpenCart 	    		           			 *
 * Release Date: 21.02.2011                                              *
 * Author: Daniel Miara                                  	             *
 * Version: 1.4.9.3                                                      *
 * For: OpenCart 					                                     *
 *                                                                       *
 * Author: 3D Studio Production                                          *
 * Email: daniel@3dstudiopro.com           			                     *
 * Website: www.3dstudiopro.com              			                 *
 * Copyright: (C) 2001-2011 All rights reserved.                         *
 * Licence: Commercial                                                   *
 *                                                                       *
 *************************************************************************
 *                                                                       *
 * This software is furnished under a license and may be used and copied *
 * only  in  accordance  with  the  terms  of such  license and with the *
 * inclusion of the above copyright notice.  This software  or any other *
 * copies thereof may not be provided or otherwise made available to any *
 * other person.  No title to and  ownership of the  software is  hereby *
 * transferred.                                                          *
 *                                                                       *
 * You may not reverse  engineer, decompile, defeat license  encryption  *
 * mechanisms, or disassemble this software product or software product  *
 * license.  3D Studio Production  may  terminate  this license  if you  *
 * fail to comply with any of the terms and conditions set forth in our  *
 * end user license agreement (EULA).  									 *
 *                                                                       *
 *                                                                       *
 * To oprogramowanie jest dostarczane w ramach licencji i może być	 	 *
 * używane i kopiowane tylko zgodnie z warunkami licencji i z 			 *
 * uwzględnieniem powyższych praw autorskich. Oprogramowania lub jego 	 * 
 * kopia nie może być przekazywana udostępnione innym trzecim.		 	 *
 *                                                                       *
 * Nie można modyfikować, dekompilować, łamać mechanizmy szyfrowania 	 *
 * tego oprogramowania lub kasować informacji o licencji oprogramowania. * 
 * 3D Studio Production może wypowiedzieć niniejszą licencję, jeśli nie  * 
 * są spełnione którekolwiek z warunków określonych w naszej umowie  	 *
 * licencyjnej użytkownika końcowego (EULA). 							 *
 *                                                                       *
 *************************************************************************
*/
class ControllerPaymentPlatnosci extends Controller {
	protected function index() {
    	$this->data['button_confirm'] = $this->language->get('button_confirm');
		$this->data['button_back'] = $this->language->get('button_back');

		$this->load->model('checkout/order');
		
		$order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']);
		
		$this->load->library('encryption');
		
		$this->data['action'] = 'https://www.platnosci.pl/paygw/UTF/NewPayment';
		
		// send
		$this->data['id'] = $this->config->get('platnosci_id');
		$this->data['auth_key'] = $this->config->get('platnosci_auth_key');
		$this->data['key1'] = $this->config->get('platnosci_key_one');
		$this->data['key2'] = $this->config->get('platnosci_key_two');
		$this->data['amount'] = $this->currency->format($order_info['total'], $order_info['currency'], $order_info['value'], FALSE);
		$this->data['order_id'] = $order_info['order_id'];
		$this->data['first_name'] = html_entity_decode($order_info['payment_firstname'], ENT_QUOTES, 'UTF-8');
		$this->data['last_name'] = html_entity_decode($order_info['payment_lastname'], ENT_QUOTES, 'UTF-8');
		$this->data['street'] = html_entity_decode($order_info['payment_address_1'], ENT_QUOTES, 'UTF-8');
		$this->data['post_code'] = html_entity_decode($order_info['payment_postcode'], ENT_QUOTES, 'UTF-8');
		$this->data['city'] = html_entity_decode($order_info['payment_city'], ENT_QUOTES, 'UTF-8');	//
		$this->data['country'] = $order_info['payment_iso_code_2'];
		$this->data['email'] = $order_info['email'];
		$this->data['phone'] = html_entity_decode($order_info['telephone'], ENT_QUOTES, 'UTF-8');
		$this->data['rows'] = $this->config->get('platnosci_rows');
		$this->data['option'] = $this->config->get('platnosci_option');
		
		if ($this->request->get['route'] != 'checkout/guest_step_3') {
			$this->data['back'] = HTTPS_SERVER . 'index.php?route=checkout/payment';
		} else {
			$this->data['back'] = HTTPS_SERVER . 'index.php?route=checkout/guest_step_2';
		}
		
		$this->id = 'payment';

		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/payment/platnosci.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/payment/platnosci.tpl';
		} else {
			$this->template = 'default/template/payment/platnosci.tpl';
		}	
		
		$this->render();
	}
	
	// only view
	public function success() {
	if (isset($this->session->data['order_id'])) {
			$order_id = $this->session->data['order_id'];
		} else {
			$order_id = 0;
		}
	
		$this->load->model('checkout/order');
		
		if (!isset($this->request->server['HTTPS']) || ($this->request->server['HTTPS'] != 'on')) {
			$this->data['base'] = HTTP_SERVER;
		} else {
			$this->data['base'] = HTTPS_SERVER;
		}
	
		$this->language->load('payment/platnosci');
		
		$order_info = $this->model_checkout_order->getOrder($order_id);
		
		$this->model_checkout_order->confirm($order_id, $this->config->get('config_order_status_id'));
	
		// heading
		$this->data['charset'] = $this->language->get('charset');
		$this->data['language'] = $this->language->get('code');
		$this->data['direction'] = $this->language->get('direction');
		//$this->data['heading_title'] = sprintf($this->language->get('heading_title'), $this->config->get('config_store'));
		//$this->data['text_response'] = $this->language->get('text_response');
		//$this->data['text_success'] = $this->language->get('text_success');
		$this->data['text_success_wait'] = sprintf($this->language->get('text_success_wait'), HTTPS_SERVER . 'index.php?route=checkout/success');
		//$this->data['text_failure'] = $this->language->get('text_failure');
		
		if ($this->request->get['route'] != 'checkout/guest_step_3') {
				$this->data['text_failure_wait'] = sprintf($this->language->get('text_failure_wait'), HTTPS_SERVER . 'index.php?route=checkout/payment');
			} else {
				$this->data['text_failure_wait'] = sprintf($this->language->get('text_failure_wait'), HTTPS_SERVER . 'index.php?route=checkout/guest_step_2');
			}
		
		
		// error
		if( isset($this->request->get['error_code']) ) {
		
		$this->data['continue'] = HTTPS_SERVER . 'index.php?route=checkout/cart';
	
				if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/payment/platnosci_failure.tpl')) {
					$this->template = $this->config->get('config_template') . '/template/payment/platnosci_failure.tpl';
					
					$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
					} else {
					$this->template = 'default/template/payment/platnosci_failure.tpl';
				}	
		
			} else { 
				
				$this->data['continue'] = HTTPS_SERVER . 'index.php?route=checkout/success';
		
				if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/payment/platnosci_success.tpl')) {
					$this->template = $this->config->get('config_template') . '/template/payment/platnosci_success.tpl';
				
					$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));	
					} else {
					$this->template = 'default/template/payment/platnosci_success.tpl';
				}
		
			}
		
	}
	// callback
	public function callback() {
	if (isset($_POST['order_id'])) {
			$order_id = $_POST['order_id'];
			} else {
			$order_id = 0;
		}

			$this->load->model('checkout/order');
			
			$server = 'www.platnosci.pl';
			$server_script = '/paygw/UTF/Payment/get';
			$pos_id = $this->config->get('platnosci_id');
			$key1 = $this->config->get('platnosci_key_one');
			$key2 = $this->config->get('platnosci_key_two');

		  function get_status($parts){
		  global $pos_id, $key1, $key2;
		  
		  if ($parts[1] != $pos_id) return array('code' => false,'message' => 'błędny numer POS'); 
		  
			  $sig = md5($parts[1].$parts[2].$parts[3].$parts[5].$parts[4].$parts[6].$parts[7].$key2);//
			  if ($parts[8] != $sig) return array('code' => false,'message' => 'błędny podpis');
			  
			}

	if( !isset($this->request->post['pos_id']) || !isset($this->request->post['session_id']) || !isset( $this->request->post['ts'] ) || !isset( $this->request->post['sig'] ) ) die('ERROR: Parametry sa puste');
	if ($this->request->post['pos_id'] != $pos_id) die('ERROR: Zly POS_ID'); 
	
	$sig = md5( $this->request->post['pos_id'] . $this->request->post['session_id'] . $this->request->post['ts'] . $key2);
	if ($this->request->post['sig'] != $sig) die('ERROR: Zla sygnatura'); 
	
	$ts = time();
	$sig = md5( $pos_id . $this->request->post['session_id'] . $ts . $key1);
	$parameters = "pos_id=" . $pos_id . "&session_id=" . $this->request->post['session_id'] . "&ts=" . $ts . "&sig=" . $sig;
	
	$fsocket = false;
	$result = false;
	$curl = true;
	
			if ($curl == true) {
				 $ch = curl_init();
				 curl_setopt($ch, CURLOPT_URL, "https://" . $server . $server_script);
				 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
				 curl_setopt($ch, CURLOPT_HEADER, 0);
				 curl_setopt($ch, CURLOPT_TIMEOUT, 20);
				 curl_setopt($ch, CURLOPT_POST, 1);
				 curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
				 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
				 $platnosci_response = curl_exec($ch);
				 curl_close($ch);
			} else {
			 die("ERROR: Brak metody łączności ...\n");
			}
		
		
		
			if (eregi("<trans>.*<pos_id>([0-9]*)</pos_id>.*<session_id>(.*)</session_id>.*<order_id>(.*)</order_id>.*<amount>([0-9]*)</amount>.*<status>([0-9]*)</status>.*<desc>(.*)</desc>.*<ts>([0-9]*)</ts>.*<sig>([a-z0-9]*)</sig>.*</trans>", $platnosci_response, $parts))  $result = get_status($parts);
			
			$result = $parts['5'];
			
			if ( $result ) { // check
			
				$pos_id = $parts[1];
				$session_id = $parts[2];
				$order_id = $parts[3];
				$amount = $parts[4];
				$status = $parts[5];
				$desc = $parts[6];
				$ts = $parts[7];
				$sig = $parts[8];
												
					if ( $result == '99' ) {
					
						$notka = 'Transakcja '. $order_id .' przebiegła pomyślnie';
						$this->model_checkout_order->update($order_id, $this->config->get('platnosci_order_zrealizowana'), $notka, TRUE);
						
					} else if ( $result == '2' ) { 
					
						$notka = 'Transakcja '. $order_id .' została anulowana';
						$this->model_checkout_order->update($order_id, $this->config->get('platnosci_order_anulowana'), $notka, TRUE);
					
					} else if ( $result == '5' || $result == '4' ) {
					
						$notka = 'Transakcja '. $order_id .' oczekuje na realizację';
						$this->model_checkout_order->update($order_id, $this->config->get('platnosci_order_nowa'), $notka, TRUE);
					
					} else if ( $result == '7' || $result == '3' ) {
					
						$notka = 'Transakcja '. $order_id .' odrzucona. Zamówienie anulowane';
						$this->model_checkout_order->update($order_id, $this->config->get('platnosci_order_anulowana'), $notka, TRUE);
					
					} else if ( $result == '888' ) {
					
						$notka = 'Błędny status transakcji '. $order_id .'. Zamówienie anulowane';
						$this->model_checkout_order->update($order_id, $this->config->get('platnosci_order_anulowana'), $notka, TRUE);
					
					}
				
							
			}	// end check
			
	echo "OK";
  }
  
	
}
?>